package com.gan.bi.api;

import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONUtil;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * @author gbw
 * @date 2023/7/17
 * 调用 OpenAI 示例
 */

public class OpenAiApi {

    public static void main(String[] args) {
        String url = "https://api.openai.com/v1/chat/completions";
        HashMap<String,Object> param = new HashMap<>();
        param.put("model","gpt-3.5-turbo");
        param.put("messages","user input");
        String json = JSONUtil.toJsonStr(param);
        String resp = HttpRequest.post(url)
                .header("Authorization", "自己的 API Key")
                .body(json)
                .execute()
                .body();
        /**
         * 得到的结果
         * {
         *   "id": "chatcmpl-123",
         *   "object": "chat.completion",
         *   "created": 1677652288,
         *   "choices": [{
         *     "index": 0,
         *     "message": {
         *       "role": "assistant",
         *       "content": "\n\nHello there, how may I assist you today?",
         *     },
         *     "finish_reason": "stop"
         *   }],
         *   "usage": {
         *     "prompt_tokens": 9,
         *     "completion_tokens": 12,
         *     "total_tokens": 21
         *   }
         * }
         */
    }
}
